package com.fzf.blog.modules.admin.service.impl;

import java.util.*;

import java.util.stream.Collectors;

import com.fzf.blog.core.util.FcContants;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.fzf.blog.modules.admin.mapper.ResourceMapper;
import com.fzf.blog.modules.admin.model.RResource;
import com.fzf.blog.modules.admin.service.ResourceService;
import tk.mybatis.mapper.entity.Example;

@Service
public class ResourceServiceImpl implements ResourceService {
    @Autowired
    private ResourceMapper resourceMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {

        return resourceMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(RResource record) {

        return resourceMapper.insert(record);
    }

    @Override
    public int insertSelective(RResource record) {

        return resourceMapper.insertSelective(record);
    }

    @Override
    public RResource selectByPrimaryKey(Integer id) {

        return resourceMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<RResource> selectLikeResourceListByPage(Map<String, Object> map) {

        return resourceMapper.selectLikeResourceListByPage(map);
    }

    @Override
    public int updateByPrimaryKeySelective(RResource record) {

        return resourceMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(RResource record) {

        return resourceMapper.updateByPrimaryKey(record);
    }

    @Override
    @Cacheable(value = FcContants.DEMO_CACHE_NAME, key = "'FC_HOME_'+#type")
    public Map<String, List<RResource>> selectAllCourse(String type) {
        Map<String, List<RResource>> maps = new HashMap<>();
        maps.put("XM", new ArrayList<>());
        maps.put("JC", new ArrayList<>());
        maps.put("GJ", new ArrayList<>());
        Example example = new Example(RResource.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("home", 1);
        List<RResource> list = resourceMapper.selectByExample(example);
        list.forEach(s -> {
            s.setPassword(null);
            s.setContent(null);
        });
        List<RResource> XM = list.stream().filter(RResource -> RResource.getType() == FcContants.ZXXM ||
                RResource.getType() == FcContants.JDXM)
                .collect(Collectors.toList());
        XM.sort(Comparator.comparing(RResource::getType).reversed());
        if (FcContants.XM.equals(type)) {
            maps.put("XM", XM);
            return maps;
        }
        List<RResource> JC = list.stream().filter(RResource -> RResource.getType() == FcContants.JCJC ||
                RResource.getType() == FcContants.JCGJ)
                .collect(Collectors.toList());
        JC.sort(Comparator.comparing(RResource::getType).reversed());
        if (FcContants.JC.equals(type)) {
            maps.put("JC", JC);
            return maps;
        }
        List<RResource> GJ = list.stream().filter(RResource -> RResource.getType() == FcContants.GJJC ||
                RResource.getType() == FcContants.GJGJ)
                .collect(Collectors.toList());
        GJ.sort(Comparator.comparing(RResource::getType).reversed());
        if (FcContants.GJ.equals(type)) {
            maps.put("GJ", GJ);
            return maps;
        }
        maps.put("XM",XM.subList(0, 6));
        maps.put("JC",JC.subList(0, 6));
        maps.put("GJ",GJ.subList(0, 6));
        return maps;
    }

    @Override
    @Cacheable(value = FcContants.DEMO_CACHE_NAME, key = "'FC_RESOURCE_'+#limit")
    public List<RResource> selectAll(Integer page, Integer limit) {
        List<RResource> list = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            PageHelper.startPage(page, limit);
            Example ex = new Example(RResource.class);
            Example.Criteria cr = ex.createCriteria();
            if (i == 0) {
                cr.andEqualTo("type", FcContants.ZXXM).orEqualTo("type", FcContants.JDXM);
            }
            if (i == 1) {
                cr.andEqualTo("type", FcContants.GJJC).orEqualTo("type", FcContants.GJGJ);
            }
            if (i == 2) {
                cr.andEqualTo("type", FcContants.JCJC).orEqualTo("type", FcContants.JCGJ);
            }
            if (i == 3) {
                cr.andEqualTo("type", FcContants.QTZY);
            }
            List<RResource> resources = resourceMapper.selectByExample(ex);
            list.addAll(resources);
        }
        return list;
    }

    @Override
    public List<RResource> selectBySearchWord(String searchWord) {
        Example example = new Example(RResource.class);
        Example.Criteria criteria = example.createCriteria();
        if (null != searchWord && !"".equals(searchWord)) {
            searchWord = "%" + searchWord + "%";
            criteria.andLike("title", searchWord).orLike("introduction", searchWord);
        }
        return resourceMapper.selectByExample(example);
    }

    @Override
    public List<?> selectResourceListByStatus() {
        return resourceMapper.selectResourceListByStatus();
    }

}
